package com.zyd.blog.file.util;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

/**
 * @author leo.jie (weixiao.me@aliyun.com)
 * @version 1.0
 * @organization bigdata
 * @website https://www.jielongping.com
 * @date 2020/5/5 1:47 下午
 * @since 1.0
 */
public class ImageUtils {
    public static final String SYSTEM_PATH = System.getProperty("java.io.tmpdir");

    public static File create(MultipartFile file, boolean isWatermark, String watermarkText) throws IOException {
        File targetFile = getTempTargetFile(Objects.requireNonNull(file.getOriginalFilename()));
        if (isWatermark) {
            BufferedImage image = handleTextWaterMark(watermarkText);
            Thumbnails.of(file.getInputStream())
                    .watermark(Positions.BOTTOM_RIGHT, image, 0.7f)
                    .outputQuality(1f)
                    .toFile(targetFile);
        } else {
            Thumbnails.of(file.getInputStream())
                    .outputQuality(1f)
                    .toFile(targetFile);
        }
        return targetFile;
    }

    public static File create(MultipartFile file, int width, int height,
                              boolean isWatermark, String watermarkText) throws IOException {
        File targetFile = getTempTargetFile(Objects.requireNonNull(file.getOriginalFilename()));
        if (isWatermark) {
            BufferedImage image = handleTextWaterMark(watermarkText);
            Thumbnails.of(file.getInputStream()).size(width, height).keepAspectRatio(false)
                    .watermark(Positions.BOTTOM_RIGHT, image, 0.7f)
                    .outputQuality(1f)
                    .toFile(targetFile);

        } else {
            Thumbnails.of(file.getInputStream()).size(width, height)
                    .keepAspectRatio(false)
                    .outputQuality(1f)
                    .toFile(targetFile);
        }
        return targetFile;
    }


    public static File createThumbnailImage(File oriFile, int width, int height,
                                            boolean isWatermark, String watermarkText) throws IOException {
        File targetFile = getTempTargetFile(oriFile.getName());
        Thumbnails.Builder<File> builder = Thumbnails.of(oriFile);
        if (isWatermark) {
            BufferedImage image = handleTextWaterMark(watermarkText);
            builder.size(width, height).keepAspectRatio(false)
                    .watermark(Positions.BOTTOM_RIGHT, image, 0.7f)
                    .outputQuality(1f)
                    .toFile(targetFile);

        } else {
            builder.size(width, height)
                    .keepAspectRatio(false)
                    .outputQuality(1f)
                    .toFile(targetFile);
        }
        return targetFile;
    }

    private static File getTempTargetFile(String fileName) {
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        String name = fileName.substring(0, fileName.lastIndexOf(suffix) - 1);
        String targetFileName = SYSTEM_PATH + name + "_" + UUID.randomUUID().
                toString().replaceAll("-", "") + "." + suffix;
        return new File(targetFileName);
    }

    public static String getShufflingFileName(String oriFileName) {
        String suffix = oriFileName.substring(oriFileName.lastIndexOf(".") + 1);
        String name = oriFileName.substring(0, oriFileName.lastIndexOf(suffix) - 1);
        return String.format("%s_shuffling.%s", name, suffix);
    }

    /**
     * @return java.awt.image.BufferedImage
     * @throws
     * @description 操纵文字型水印
     * @params [address, dealerName, latitudeCommaLongitude, date]
     */
    private static BufferedImage handleTextWaterMark(String watermarkText) {

        final Font font = new Font("微软雅黑", Font.ITALIC, 18);
        BufferedImage image = new BufferedImage(300, 50, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = image.createGraphics();
        image = g.getDeviceConfiguration().createCompatibleImage(300, 50, Transparency.TRANSLUCENT);
        int y = 0;
        int divider30 = 30;
        g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(new Color(236, 114, 89));
        g.setFont(font);
        g.drawString(watermarkText, 5, y + divider30);
        g.dispose();
        return image;
    }

    public static void main(String[] args) throws IOException {
        File oriFile = new File("/Users/mac/Desktop/images/eagle.jpeg");

        System.out.println(createThumbnailImage(oriFile, 1190, 300,
                true, "https://www.jielongping.com"));
    }
}
